<template>
  <div class="top">
    <div class="header-left">
      <!-- 全屏模式 开/关 -->
      <div class="full-screen">
        <i class="el-icon-rank" @click="toggleScreen"></i>
      </div>
      <!-- 消息通知提醒 -->
      <router-link class="notice" to="/home/message">
        <!-- <el-tooltip effect="dark" content="未读消息" placement="right">
          <i class="el-icon-bell">
            <label>11</label>
          </i>
        </el-tooltip> -->
      </router-link>
    </div>
    <div class="header-right">
      <!-- 用户下拉菜单 个人信息  登出-->
      <el-dropdown trigger="click" class="drop-down" @command="handleCommand">
        <!-- 用户名 -->
        <span class="el-dropdown-link">
          {{ userName }}
          <!-- 下拉图标 -->
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <!-- 下拉框内容 -->
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
          <el-dropdown-item command="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="clean-both"></div>
  </div>
</template>
<script>
export default {
  name: 'headNav',
  data () {
    return {
      // 用户名
      userName: this.$store.state.userinfo.userName,
      // 全屏状态
      fullscreen: false
    };
  },
  methods: {
    // 获取个人信息 登出
    handleCommand (command) {
      let _this = this;
      if (command === 'userinfo') {
        // 个人信息页面
        _this.$router.push('/home/userinfo');
      } else if (command === 'logout') {
        let params = {
          userId: _this.$store.state.userinfo.userId
        };
        // 服务器端退出账户
        _this.$http.post('api/loginOut', params).then(res => {
          let { code } = res;
          // 判断Code状态
          if (code === 0) {
            // 登出 - 清除vuex
            localStorage.clear();
            sessionStorage.clear();
            // 提示信息
            _this.$message({
              type: 'success',
              message: '退出成功！'
            });
            _this.$router.push('/login');
          }
        }).catch(err => {
          if (err === undefined) {
            _this.$message({
              message: '服务器响应异常！',
              type: 'error'
            });
            return false;
          }
        });
      }
    },

    // 网页全屏模式
    toggleScreen () {
      // 绑定Dom
      let element = document.documentElement;
      // 判断当前状态
      if (this.fullscreen) {
        // 判断当前浏览器内核
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen();
        }
      }
      // 修改状态值
      this.fullscreen = !this.fullscreen;
    }
  }
};
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
